package hospital.action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import hospital.model.UserInfoModel;
import hospital.service.LoginService;
import hospital.util.HospitalUtil;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

public class UserLogin extends ActionSupport implements Preparable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5028734917196038731L;
	private String username;
	private String password;
	private LoginService loginService;
	private HospitalUtil hospitalUtil;

	public String login() {

		UserInfoModel userInfo = loginService.getUserInfo(username);
		byte role = userInfo.getRole();
		long userId = userInfo.getUserId();
		String firstname = userInfo.getFirstname();
		String lastname = userInfo.getLastname();
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (role == 0) {
			session.setAttribute("currentUserId", userId);
			session.setAttribute("currentName", firstname + " " + lastname);
			return "patient";
		} else if (role == 1) {
			session.setAttribute("currentUserId", userId);
			session.setAttribute("currentName", firstname + " " + lastname);
			return "doctor";
		} else if (role == 2) {
			session.setAttribute("currentUserId", userId);
			session.setAttribute("currentName", firstname + " " + lastname);
			return "staff";
		} else if (role == 3) {
			session.setAttribute("currentUserId", userId);
			session.setAttribute("currentName", firstname + " " + lastname);
			return "legal";
		} else if (role == 4) {
			session.setAttribute("currentUserId", userId);
			session.setAttribute("currentName", firstname + " " + lastname);
			return "financial";
		} else if (role == 5) {
			session.setAttribute("currentUserId", userId);
			session.setAttribute("currentName", firstname + " " + lastname);
			return "admin";
		} else {
			addActionError("Sorry, you have no right to access yet.");
			return INPUT;
		}

	}

	public String logout() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute("currentUserId");
		session.removeAttribute("currentName");
		session.removeAttribute("yearList");
		session.removeAttribute("monthList");
		session.removeAttribute("dayList");
		session.removeAttribute("hourList");
		session.removeAttribute("minuteList");
		session.removeAttribute("genderList");
		return SUCCESS;
	}

	@Override
	public void prepare() throws Exception {
		clearFieldErrors();
		clearActionErrors();
	}

	public void validateLogin() {

		String pw = loginService.getPassword(username);
		String encryptedPw = hospitalUtil.encrypt(password);
		if (pw == null || !pw.equals(encryptedPw)) {
			addActionError("Incorrect username or password");
		}
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public LoginService getLoginService() {
		return loginService;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	public HospitalUtil getHospitalUtil() {
		return hospitalUtil;
	}

	public void setHospitalUtil(HospitalUtil hospitalUtil) {
		this.hospitalUtil = hospitalUtil;
	}

}
